/*-
 * Copyright (c) 2006, 2007 FTS Team
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 4. Neither the name of the FTS Team nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *      Indexer.cpp
 *
 * $FTS$
 */
#include <Indexer.hpp>

#include <stdlib.h>
#include <stdio.h>
namespace FTSS // FullTextSearchSystem
{

//
// Constructor
//
Indexer::Indexer(InvIndexBPlusStorage * pIStorage,
                 const UINT_32 &   iDictSize): pStorage(pIStorage),
                                               iDocumentId(0),
                                               iDocumentNum(0),
                                               iDocumentCount(0),
                                               oIndexHash(iDictSize)
{
	;;
}

//
// Start indexing
//
void Indexer::StartIndexer(const DocumentId_t & iStartDocumentId)
{
//	oIndexHash.clear();
	iDocumentCount = 0;
	iDocumentNum = iStartDocumentId;
}

//
// Start new document
//
DocumentId_t Indexer::StartDocument(CCHAR_P sDocName, const UINT_32 & iDocNameLen)
{
#ifdef _DEBUG
fprintf(stderr, "StartDocument `%s', ID: %d\n", sDocName, INT_32(iDocumentNum));
#endif

return (iDocumentId = ++iDocumentNum);
}

//
// Store word in index
//
void Indexer::PushWord(const TokenHash_t   & iWordHash,
	               const WordForm_t    & iWordForm,
	               const ZoneId_t      & iZoneId,
	               const DocumentPos_t & iWordPos)
{
	IndexHash::iterator ithData = oIndexHash.find(iWordHash, iWordHash);

	// Word found in index
	if (ithData != oIndexHash.end())
	{
		ithData -> second.push_back(WordEntry(iWordPos, iWordForm, iZoneId));
	}
	else
	{
#ifdef _NO_FTSS_STL_ANALOGS
		STLW::vector<WordEntry> vTMP;
#else
		DocumentEntryContainer vTMP;
#endif // _NO_FTSS_STL_ANALOGS

		vTMP.push_back(WordEntry(iWordPos, iWordForm, iZoneId));

		oIndexHash.insert(IndexPair(iWordHash, vTMP), iWordHash);
	}
}

//
// End of document
//
void Indexer::EndDocument()
{
	IndexHash::iterator ithData = oIndexHash.begin();

#ifdef _NO_FTSS_STL_ANALOGS
	Size_t iRawDataSize = 0;
	void * vRawData     = NULL;
#endif // _NO_FTSS_STL_ANALOGS

	while (ithData != oIndexHash.end())
	{
		if (ithData -> second.size() != 0)
		{
#ifdef _NO_FTSS_STL_ANALOGS
			Size_t iCurrRawDataSize = sizeof(DocumentEntry) + (ithData -> second.size() - 1) * sizeof(WordEntry);
			if (iCurrRawDataSize > iRawDataSize)
			{
				if (vRawData != NULL) { ::operator delete(vRawData); }

				iRawDataSize = iCurrRawDataSize;
				vRawData = ::operator new(iRawDataSize);
			}
			DocumentEntry * pDocEntry = (DocumentEntry *)vRawData;

			pDocEntry -> doc_id = iDocumentId;
			pDocEntry -> size   = iCurrRawDataSize;
			for (Size_t iI = 0; iI < ithData -> second.size(); iI++) { pDocEntry -> words[iI] = ithData -> second[iI]; }
			pStorage -> StoreDocumentEntry(ithData -> first, pDocEntry, iCurrRawDataSize);

			// Not need
			//{ STLW::vector<WordEntry> vTMP; ithData -> second.swap(vTMP); }
			ithData -> second.clear(); // Need to test performance
#else
			ithData -> second.StoreDocumentId(iDocumentId);
			pStorage -> StoreDocumentEntry(ithData -> first, ithData -> second.GetData(), ithData -> second.GetData() -> size);

			// Not need
			// { DocumentEntryContainer vTMP; ithData -> second.swap(vTMP); }
			ithData -> second.erase(); // Need to test performance
#endif // _NO_FTSS_STL_ANALOGS
		}
		++ithData;
	}
	++iDocumentCount;
	iDocumentId = 0;

#ifdef _NO_FTSS_STL_ANALOGS
	if (vRawData != NULL) { ::operator delete(vRawData); }
#endif // _NO_FTSS_STL_ANALOGS
}

//
// Finish of entire document collection
//
void Indexer::StopIndexer()
{
	pStorage -> StoreDocNum(iDocumentCount);
}

//
// A Destructor
//
Indexer::~Indexer() throw()
{	
}

} // namespace FTSS
// End.
